<?php
/**
 * Created by PhpStorm.
 * User: jianglong
 * Date: 2017/11/10
 * Time: 上午11:37
 */

namespace app\message\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\manager\admin\ManagerException;
use think\Db;

/**
 * 客户，领导消息管理
 * Class index
 * @package app\message\admin
 */
class index extends Admin
{
    /**
     * 领导已发送的消息
     */
    function sendedMsg()
    {

    }

    /**
     * 客户经理待浏览消息
     */
    function waitScan()
    {
        $a_fields = [
            'a.id id',
            'a.type type',
            'a.title title',
            'a.time time',
            'b.name name'
        ];
        $a_where = [
            'a.status' => 0,
        ];
        if (ROLE_ID !== 1):
            $a_where['manager_id'] = Db::name('admin_user')->where('id', UID)->value('role_id') ?: 0;
        endif;
        $a_waitMessage = Db::name('event_log a')->field($a_fields)->join('admin_user c', 'c.id=a.send_id')->join('manager b', 'c.role_id=b.id', 'LEFT')->where($a_where)->order('a.time desc')->paginate();
        return ZBuilder::make('table')
            ->setPageTips('1.待读领导消息是领导向客户经理发送的消息<br/>2.领导消息记录是客户经理已经查看过的由领导发送的消息')
            ->hideCheckbox()
            ->addColumns([
                ['__INDEX__', '序号'],
                ['type', '消息类型', '', '', config('message_type')],
                ['title', '消息标题'],
                ['time', '发送时间', 'datetime'],
                ['name', '发送人', '', '超级管理员'],
                ['right_button', '操作', 'btn']
            ])
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('showMsgDetail', ['id' => '__id__'])
                ], true)
            ->setRowList($a_waitMessage)
            ->fetch();
    }

    //消息历史（已查看过的消息)
    function showMsgPast()
    {
        $a_fields = [
            'a.id id',
            'a.type type',
            'a.title title',
            'a.time time',
            'b.name name'
        ];
        $a_where = [
            'a.status' => 1,
        ];
        if (ROLE_ID != 1):
            $a_where['manager_id'] = Db::name('admin_user')->where('id', UID)->value('role_id') ?: 0;
        endif;
        $a_where = $this->getMap() + $a_where;
        if (isset($a_where['event_log.type'])):
            $a_where['a.type'] = $a_where['event_log.type'];
            unset($a_where['event_log.type']);
        endif;
        if (ROLE_ID == 1):
            $a_where['send_id'] = 1;
            $i_type = input("type/d", 0);
            if ($i_type !== 0):
                $a_where['a.status'] = $i_type === 1 ? 1 : 0;
            endif;
        endif;
        $a_waitMessage = Db::name('event_log a')->field($a_fields)->join('admin_user c', 'c.id=a.send_id')->join('manager b', 'c.role_id=b.id', 'LEFT')->where($a_where)->order('a.time desc')->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumns([
                ['__INDEX__', '序号'],
                ['type', '消息类型', '', '', config('message_type')],
                ['title', '消息标题'],
                ['time', '发送时间', 'datetime'],
                ['name', '发送人', '', '超级管理员'],
                ['right_button', '操作', 'btn']
            ])
            ->addFilter([
                'type' => 'event_log'
            ], config('message_type'))
            ->setSearch([
                'title' => "消息标题"
            ])
            ->addTimeFilter('time')
            ->setTableName('event_log')
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('showMsgDetail', ['id' => '__id__'])
                ], true)
            ->setRowList($a_waitMessage)
            ->fetch();
    }

    /**
     * 系统消息
     * @param $i_status int 状态
     * @return mixed
     */
    public function sysMsg($i_status = 0)
    {
        $a_fields = [
            'a.id id',
            'a.type type',
            'a.title title',
            'a.time time',
            'a.status status',
            'b.name manager_name'
        ];
        $a_where = [
            'a.status' => $i_status,
            'send_id' => 0,
            'manager_id' => Db::name('admin_user')->where('id', UID)->value('role_id') ?: 0,
        ];
        if (ROLE_ID == 1):
            unset($a_where['manager_id']);
        elseif(ROLE_ID == 2):
            //领导查看自己管理的客户经理待读的系统消息
            $s_netIds = Db::name('manager')->where('id', LEADER_ID)->value('net_name');
            $a_netName = Db::name('netsite_record')->where('id', 'in', $s_netIds)->column('net_name');
            if (!empty($a_netName)):
                $a_managerIds = Db::name('manager')->where('net_name', 'in', $a_netName)->column('id');
                $a_where['manager_id'] = ['in', $a_managerIds];
            else:
                $a_where['manager_id'] = -1;
            endif;
        endif;
        $a_where = $this->getMap() + $a_where;
        if (isset($a_where['event_log.type'])):
            $a_where['a.type'] = $a_where['event_log.type'];
            unset($a_where['event_log.type']);
        endif;
        $a_waitMessage = Db::name('event_log a')->field($a_fields)
            ->join('manager b', 'a.manager_id=b.id')
            ->where($a_where)
            ->order('a.time desc')->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->setPageTips('1.待读系统消息是系统向客户经理发送的消息<br/>2.系统消息记录是客户经理已经查看过的由系统发送的消息')
            ->addColumns([
                ['__INDEX__', '序号'],
                ['type', '消息类型', '', '', config('message_type')],
                ['title', '消息标题'],
                ['time', '发送时间', 'datetime'],
                ['name', '发送人', '', '系统'],
                ['manager_name', '接收人', ''],
                ['status', '消息状态', 'status', '', ['未查看', '已查看']],
                ['right_button', '操作', 'btn']
            ])
            ->addFilter([
                'type' => 'event_log'
            ], config('message_type'))
            ->setSearch([
                'title' => "消息标题"
            ])
            ->addTimeFilter('time')
            ->setTableName('event_log')
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('scanMsgDetail', ['id' => '__id__']),
                ], true)
            ->setRowList($a_waitMessage)
            ->fetch();
    }

    /**
     * 系统消息历史
     */
    public function sysMsgHistory()
    {
        return $this->sysMsg(1);
    }

    /**
     * 消息详情
     * @return mixed
     */
    function showMsgDetail()
    {
        $i_id = input('id/d', 0);
        $a_msg = Db::name('event_log')->where('id', $i_id)->find();
        if ($a_msg['send_id'] == 1):
            $s_send = '超级管理员';
        elseif ($a_msg['send_id'] == 0):
            $s_send = '系统';
        else:
            $i_roleId = Db::name('admin_user')->where('id', $a_msg['send_id'])->value('role_id');
            $s_send = Db::name('manager')->where('id', $i_roleId)->value('name');
        endif;
        //变更消息桩体为已阅读,超级管理员除外
        //if (ROLE_ID !== 1) Db::name('event_log')->where('id', $i_id)->update(['status' => 1]);
        $this->assign([
            'send' => $s_send,//发送人
            'time' => date('Y-m-d H:i:s', $a_msg['time']),//发送时间
            'title' => $a_msg['title'],//消息标题
            'content' => $a_msg['content'],//消息内容
            'id' => $i_id,
            'status' => $a_msg['status'],
            'response' => $a_msg['response'],
            'role' => ROLE_ID,
        ]);
        return $this->fetch();
    }

    /**
     * 领导查看消息
     */
    public function showMsgList()
    {
        $a_fields = [
            'a.id id',
            'a.type type',
            'a.title title',
            'a.time time',
            'b.name name',
            'a.status status'
        ];
        $a_where = [
            'send_id' => UID
        ];
        $i_type = input('?type') ? input('type/d') : 0;
        switch ($i_type):
            case 0:
                break;
            case 1:
                $a_where['a.status'] = 1;
                break;
            case 2:
                $a_where['a.status'] = 0;
                break;
        endswitch;
        $a_waitMessage = Db::name('event_log a')->field($a_fields)->join('manager b', 'a.manager_id=b.id')->where($a_where)->order('a.time desc')->paginate();
        return ZBuilder::make('table')
            ->setPageTips('点击操作栏按钮可查看消息详情')
            ->hideCheckbox()
            ->addColumns([['__INDEX__', '序号'],
                ['type', '消息类型', '', '', config('message_type')],
                ['title', '消息标题'],
                ['time', '发送时间', 'datetime'],
                ['name', '接收人'],
                ['status', '消息状态', 'status', '', ['未查看', '已查看']],
                ['right_button', '操作', 'btn']])
            ->addRightButton('custom',
                ['title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('scanMsgDetail', ['id' => '__id__'])], true)
            ->setRowList($a_waitMessage)
            ->fetch();
    }

    /**
     * 领导查看消息历史
     * @return mixed
     */
    function scanMsgDetail()
    {
        $i_id = input('id/d', 0);
        $a_msg = Db::name('event_log')->where('id', $i_id)->find();
        if (ROLE_ID != 1):
            $a_where = [
                'id' => $a_msg['manager_id']
            ];
        else:
            $a_where = [];
        endif;
        if (ROLE_ID == 3 && $a_msg):
            Db::name('event_log')->where('id', $i_id)->update(['status' => 1]);
        endif;
        $s_receiver = Db::name('manager')->where($a_where)->value('name');
        //变更消息桩体为已阅读
        $this->assign([
            'receiver' => $s_receiver,
            'time' => date('Y-m-d H:i:s', $a_msg['time']),//发送时间
            'title' => $a_msg['title'],//消息标题
            'content' => $a_msg['content'],//消息内容
        ]);
        return $this->fetch();
    }

    /**
     * 客户移交通知
     */
    function showCusTurn()
    {
        $a_where = [
            'a.next_log' => 0,
            'a.status' => ['in', '1,2'],
        ];
        $a_manager = Db::name('manager')->column('id,name');
        $i_roleId = Db::name('admin_user')->where('id', UID)->value('role_id');
        if (ROLE_ID != 1):
            $a_where['a.manager_id|a.old_manager'] = $i_roleId;
        endif;
        $a_where = $this->getMap() + $a_where;
        //处理字段名冲突
        if (isset($a_where['manager_id'])):
            $a_where['a.manager_id'] = $a_where['manager_id'];
            unset($a_where['manager_id']);
        endif;
        $a_fields = [
            'a.id id', 'a.status status', 'b.name customer_name', 'a.old_manager', 'a.manager_id',
            'a.start_time', 'a.time'
        ];
        $a_data = Db::name('customer_turn_log a')->field($a_fields)
            ->join('customer b', 'a.customer_id=b.id')
            ->order('a.id desc')
            ->where($a_where)
            ->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumns([
                ['__INDEX__', '序号'],
                ['customer_name', '移交客户'],
                ['old_manager', '原客户经理', $a_manager + [0 => '']],
                ['manager_id', '移交客户经理', $a_manager],
                ['status', '移交状态', 'status', '', ['正在移交', '移交完成', '拒绝接收']],
                ['time', '完成时间', 'datetime', ''],
            ])
            ->addTimeFilter('time')
            ->setTableName('customer_turn_log')
            ->setSearch([
                'b.name' => '移交客户',
            ])
            ->addFilter([
                'old_manager',
                'manager_id'
            ], $a_manager)
            ->addFilter('status', ['正在移交', '移交完成', '拒绝接收'])
            ->addFilterMap('status', ['status' => ['neq', 0]])
            ->addFilterMap('old_manager', ['old_manager' => ['neq', 0]])
            ->addRightButton('custom', [
                'title' => '查看详情',
                'icon' => 'glyphicon glyphicon-eye-open',
                'href' => url('')
            ])
            ->setRowList($a_data)
            ->fetch();
    }

    /**
     * 移交待接收客户
     */
    function waitReccus()
    {
        $a_where = [
            'a.status' => 0,
            'a.next_log' => 0,
        ];
        if (ROLE_ID != 1):
            $a_where['a.manager_id'] = Db::name('admin_user')->where('id', UID)->value('role_id');
        endif;
        $a_map = $this->getMap();
        foreach ($a_map as $s_key => $a_val):
            switch ($s_key):
                case 'customer_turn_log.old_manager':
                    $a_map['a.old_manager'] = $a_val;
                    unset($a_map[$s_key]);
                    break;
                case 'customer_turn_log.customer_id':
                    $a_map['a.customer_id'] = $a_val;
                    unset($a_map[$s_key]);
                    break;
            endswitch;
        endforeach;
        $a_where = array_merge($a_where, $a_map);

        //查询字段
        $a_fields = [
            'a.id id', 'a.status status', 'c.name manager_name',
            'a.old_manager', 'a.customer_id customer_id',
            'a.start_time', 'a.time'
        ];
        $a_data = Db::name('customer_turn_log a')->field($a_fields)
            ->join('manager c', 'a.manager_id=c.id')
            ->order('a.id desc')
            ->where($a_where)->paginate();
        //所有经理
        $a_manager = Db::name('manager')->column('id,name');
        //当前移交客户信息
        $a_customerIds = array_map(function ($a_val) {
            return $a_val['customer_id'];
        }, $a_data->toArray()['data']);
        $a_customer = Db::name('customer')->where(['id' => ['in', $a_customerIds]])->column('id,name');
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addFilter([
                'old_manager' => 'customer_turn_log',
            ], $a_manager)
            ->addFilter([
                'customer_id' => 'customer_turn_log',
            ], $a_customer)
            ->addFilterMap('customer_id', ['customer_id' => ['in', join(',', $a_customerIds)]])
            ->addTimeFilter('start_time', '', ['申请开始', '申请结束'])
            ->addColumns([
                ['customer_id', '移交客户', $a_customer],
                ['old_manager', '申请客户经理', $a_manager],
                ['manager_name', '移交客户经理'],
                ['status', '移交状态', 'status', '', ['正在移交', '移交完成']],
                ['start_time', '申请时间', 'datetime'],
                ['right_button', '操作', 'btn'],
            ])
            ->addRightButton('custom', [
                'title' => '查看客户移交历史',
                'icon' => 'glyphicon glyphicon-eye-open',
                'href' => url('dealTurn', [
                    'customer_id' => '__customer_id__',
                ])
            ])
            ->addRightButton('custom', [
                'title' => '确认接收',
                'icon' => 'fa fa-fw fa-check',
                'class' => 'btn btn-xs btn-success  ajax-get confirm',
                'href' => url('dealTurn', [
                    'customer_id' => '__customer_id__',
                    'id' => '__id__',
                    'status' => 1,
                ]),
                'data-title' => '确认接收该客户？'
            ])
            ->addRightButton('custom', [
                'title' => '拒绝接收',
                'icon' => 'fa fa-fw fa-close',
                'class' => 'btn btn-xs btn-danger text-danger ajax-get confirm',
                'href' => url('dealTurn', [
                    'customer_id' => '__customer_id__',
                    'id' => '__id__',
                    'status' => 2,
                ]),
                'data-title' => '拒绝该客户？'
            ])
            ->setRowList($a_data)
            ->fetch();
    }

    /**
     * 客户经理接收或者拒绝接收移交客户
     */
    function dealTurn()
    {
        if (request()->isAjax()):
            $i_status = input('status/d');
            $i_customerId = input('customer_id/d');
            $i_logId = input('id/d');
            $a_where = [
                'customer_id' => $i_customerId,
                'manager_id' => MANAGER_ID,
                'status' => 0,
                'next_log' => 0,
            ];
            $a_log = $a_log = Db::name('customer_turn_log')->where($a_where)->find();
            if (empty($a_log) || $a_log['id'] != $i_logId):
                $this->error("操作失败，客户移交状态错误！");
            endif;
            Db::startTrans();
            try {
                //插入新的日志记录
                $s_message = "客户经理同意移交客户";
                $s_message = $i_status == 1 ? $s_message : '客户经理拒绝接收客户';
                $a_insertData = [
                    'operate_id' => UID,
                    'customer_id' => $i_customerId,
                    'manager_id' => MANAGER_ID,
                    'old_manager' => $a_log['old_manager'],
                    'start_time' => $a_log['start_time'],
                    'time' => $_SERVER['REQUEST_TIME'],
                    'status' => $i_status,
                    'message' => $s_message,
                    'next_log' => 0,

                ];
                $i_id = Db::name('customer_turn_log')->insertGetId($a_insertData);
                if (!$i_id):
                    throw new ManagerException("移交失败！");
                endif;
                //更新原日志
                if (!Db::name('customer_turn_log')->where('id', $i_logId)->update(['next_log' => $i_id])):
                    throw new ManagerException("移交更新失败！");
                endif;
                //确认移交
                $a_updateData = [
                    'turn_status' => 1
                ];
                if ($i_status == 1):
                    $a_updateData['manager_id'] = MANAGER_ID;
                endif;
                //更新客户所属客户经理
                if (!Db::name('customer')->where('id', $i_customerId)->update($a_updateData)):
                    throw new ManagerException("更新客户经理失败！");
                endif;
                Db::commit();
                $this->success("操作成功！");
            } catch (ManagerException $e) {
                Db::rollback();
                $this->error($e->getErrorMsg());
            }
        else:
            $i_customerId = input('customer_id/d', 0);
            $a_where = [
                'status' => 0,
                'next_log' => 0,
                'customer_id' => $i_customerId
            ];
            $a_data = Db::name('customer_turn_log')->where($a_where)->find();
            if (empty($a_data)):
                $this->error("移交状态错误！");
            endif;
            if (ROLE_ID !== 1 && $a_data['manager_id'] != MANAGER_ID):
                $this->error("当前身份不是待移交经理！");
            endif;

            //该客户的所有移交日志
            $a_allLog = Db::name('customer_turn_log')
                ->order('id desc')
                ->where(['customer_id' => $i_customerId])
                ->paginate();
            $a_manager = Db::name('manager')->column('id,name');
            $s_customerName = Db::name('customer')->where('id', $i_customerId)->value('name');
            return ZBuilder::make('table')
                ->hideCheckbox()
                ->addColumns([
                    ['name', '客户姓名', 'text', $s_customerName],
                    ['old_manager', '原客户经理', $a_manager],
                    ['manager_id', '移交客户经理', $a_manager],
                    ['status', '移交状态', 'status', '', ['正在移交', '移交完成', '移交失败']],
                    ['start_time', '时间', 'datetime']
                ])
                ->setRowList($a_allLog)
                ->fetch();
        endif;
    }

    /**
     * 2018年03月15日10:24:30
     * 消息统计
     * author jianglong
     */
    public function netstat()
    {
        $a_rsl = Db::name('event_log')->where('send_id', UID)->group('status')->column('count(*) count', 'status');
        $a_rsl[0] = isset($a_rsl[0]) ? $a_rsl[0] : 0;
        $a_rsl[1] = isset($a_rsl[1]) ? $a_rsl[1] : 0;
        $a_rsl = [
            [
                'id' => 1,//全部
                'type' => 0,
                'count' => $a_rsl[0] + $a_rsl[1],
            ],
            [
                'id' => 1,//已查看
                'type' => 1,
                'count' => $a_rsl[1]
            ],
            [
                'id' => 0,//未查看
                'type' => 2,
                'count' => $a_rsl[0]
            ],

        ];
        switch (ROLE_ID):
            case 1:
                /*超级管理员消息统计*/
            case 2:
                /*领导消息统计 */
                return ZBuilder::make("table")
                    ->hideCheckbox()
                    ->addColumns([
                        ['type', '消息类别', 'status', '', ['全部消息:info', '已被查看', '未被查看:warning']],
                        ['count', '消息条数'],
                        ['right_button', '操作', 'btn']
                    ])
                    ->setRowList($a_rsl)
                    ->addRightButton('customer1', [
                        'title' => '查看消息列表',
                        'icon' => 'glyphicon glyphicon-eye-open',
                        'class' => 'btn btn-xs',
                        'href' => ROLE_ID == 2 ? url('receiveLeaderMsg') : url('receiveAdminMsg')
                    ])
                    ->addRightButton('customer2', [
                        'title' => '查看消息列表',
                        'icon' => 'glyphicon glyphicon-eye-open',
                        'class' => 'btn btn-xs',
                        'href' => ROLE_ID == 2 ? url('leaderMsgRe') : url('AdminMsgRe')
                    ])
                    ->replaceRightButton(['type' => 0], '', 'customer1,customer2')
                    ->replaceRightButton(['type' => 1], '', 'customer1')
                    ->replaceRightButton(['type' => 2], '', 'customer2')
                    ->fetch();
                break;
            /*客户经理消息统计*/
            case 3:

                break;
        endswitch;

    }


    /*待读领导消息*/
    public function receiveLeaderMsg($i_status = 0)
    {
        $a_fields = [
            'a.id id',
            'a.type type',
            'a.title title',
            'a.manager_id manager_id',
            'a.time time',
            'b.name name',
            'a.status status'
        ];
        $a_where = [
            'a.status' => $i_status,
            'a.send_id' => ['not in', [0, 1]]
        ];
        //客户经理查看自己的领导消息
        if (ROLE_ID == 3):
            $a_where['a.manager_id'] = MANAGER_ID;
        endif;
        //领导查看自己的领导消息
        if (ROLE_ID == 2):
            $a_where['a.send_id'] = UID;
        endif;
        //查看及管理员查看所有未读领导消息
        if (ROLE_ID == 1):
            unset($a_where['send_id']);
            $a_where['send_id'] = ['not in', [0, 1]];
        endif;

        $a_waitMessage = Db::name('event_log a')->field($a_fields)->join('admin_user c', 'c.id=a.send_id')->join('manager b', 'c.role_id=b.id', 'LEFT')->where($a_where)->order('a.time desc')->paginate();
        //接收客户经理
        $a_managerIds = array_map(function ($a_item) {
            return $a_item['manager_id'];
        }, $a_waitMessage->toArray()['data'] ?: []);
        return ZBuilder::make('table')
            ->setPageTips('1.待读领导消息是领导向客户经理发送的消息<br/>2.领导消息记录是客户经理已经查看过的由领导发送的消息')
            ->hideCheckbox()
            ->addColumns([
                ['__INDEX__', '序号'],
                ['type', '消息类型', '', '', config('message_type')],
                ['title', '消息标题'],
                ['time', '发送时间', 'datetime'],
                ['name', '发送人', '', '超级管理员'],
                ['manager_id', '接收人', '', '', Db::name('manager')->where('id', 'in', $a_managerIds)->column('name', 'id')],
                ['status', '消息状态', 'status', '', ['未回复', '已回复']],
                ['right_button', '操作', 'btn']
            ])
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('showMsgDetail', ['id' => '__id__'])
                ], true)
            ->addRightButton('custom2',
                [
                    'title' => '回复消息',
                    'icon' => 'fa fa-fw fa-comments',
                    'href' => url('responseLeaderMsg', ['id' => '__id__'])
                ])
            ->replaceRightButton(['status' => 1], '', 'custom2')
            ->setRowList($a_waitMessage)
            ->fetch();
    }

    /*领导消息记录*/
    public function leaderMsgRe()
    {
        return $this->receiveLeaderMsg(1);
    }

    /**
     * 2018年03月15日16:52:13
     * author jianglong
     * 客户经理回复领导消息
     */
    public function responseLeaderMsg()
    {
        if ($this->request->isAjax()):
            $i_id = input('id/d');
            $s_content = input('content');
            if (empty($s_content) || empty($i_id)):
                $this->error("回复内容不能为空！");
            endif;
            if (Db::name('event_log')->where(['id' => $i_id, 'status' => 0])->update(['status' => 1, 'response' => $s_content])):
                $this->success("回复成功！", url('receiveLeaderMsg'));
            else:
                $this->error("回复失败！");
            endif;
        else:
            return ZBuilder::make('form')
                ->addHidden('id', input('id'))
                ->addFormItem('wangeditor', 'content')
                ->fetch();
        endif;
    }

    /**
     * 2018年03月15日14:36:41
     * author jianglong
     * 待读超级管理员消息
     */
    public function receiveAdminMsg($i_status = 0)
    {
        $a_fields = [
            'a.id id',
            'a.type type',
            'a.title title',
            'a.manager_id manager_id',
            'a.time time',
            'a.status status'
        ];
        $a_where = [
            'a.status' => $i_status,
            'a.send_id' => 1,
        ];
        //客户经理只能查看自己的系统消息
        if (ROLE_ID == 3):
            $a_where['a.manager_id'] = MANAGER_ID;
        elseif (ROLE_ID == 2):
            unset($a_where['a.manager_id']);
        endif;
        $a_waitMessage = Db::name('event_log a')->field($a_fields)->where($a_where)->order('a.time desc')->paginate();
        //接收客户经理
        $a_managerIds = array_map(function ($a_item) {
            return $a_item['manager_id'];
        }, $a_waitMessage->toArray()['data'] ?: []);
        return ZBuilder::make('table')
            ->setPageTips('1.待读管理员消息是系统超级管理员向客户经理发送的消息<br/>2.管理员消息记录是客户经理已经查看过的由超级管理员发送的消息')
            ->hideCheckbox()
            ->addColumns([
                ['__INDEX__', '序号'],
                ['type', '消息类型', '', '', config('message_type')],
                ['title', '消息标题'],
                ['time', '发送时间', 'datetime'],
                ['name', '发送人', '', '超级管理员'],
                ['manager_id', '接收人', '', '', Db::name('manager')->where('id', 'in', $a_managerIds)->column('name', 'id')],
                ['status', '消息状态', 'status', '', ['未查看', '已查看']],
                ['right_button', '操作', 'btn']
            ])
            ->addRightButton('custom',
                [
                    'title' => '查看详情',
                    'icon' => 'glyphicon glyphicon-eye-open',
                    'href' => url('scanMsgDetail', ['id' => '__id__'])
                ], true)
            ->setRowList($a_waitMessage)
            ->fetch();
    }

    /**
     * 2018年03月15日14:37:18
     * author jianglong
     * 超级管理员消息记录
     */
    public function adminMsgRe()
    {
        return $this->receiveAdminMsg(1);
    }
}
